package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Category;
import com.itheima.bean.Dish;
import com.itheima.bean.Setmeal;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.DishDao;
import com.itheima.dao.SetmealDao;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishDao dishDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public boolean deleteWithDishAndSetmeal(Long categoryId) {
        //1.判断当前分类下是否关联了菜品
        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Dish::getCategoryId,categoryId);
        Integer count = dishDao.selectCount(wrapper1);
        if(count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除！");
        }


        //2.判断当前分类下是否关联了套餐
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId,categoryId);
        Integer count1 = setmealDao.selectCount(wrapper2);
        if(count1>0){
            throw new CustomException("当前分类下关联了套餐，不能删除！");
        }

        //3.如果该分类下没有关联套餐或菜品，则执行删除
        boolean flag = this.removeById(categoryId); //this：表示当前类对象  removeById()：就是MyBatisPlus在service提供的删除功能的通用方法

        return flag;
    }
}
